<?php
namespace App\Http\Controllers\Api\Coupon;

use App\Http\Controllers\Controller;
use App\User;
use App\UserCoinLog;
use App\GameZghpt;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Jobs\SendWechatMessage;

/**
 * 优惠卷功能
 *
 * @author  Kay
 * @version 1.0.0
 */
class CouponController extends Controller
{

    /**
     * 商品优惠卷
     * @param Request $request
     * @return mixed
     */
    public function goodsCoupon(Request $request)
    {
        $goods_id = $request->input('goods_id'); //当前商品ID
        // 检查请求参数
        if (empty($goods_id)) {
            $data['code'] = 1;
            $data['msg'] = '请求参数不合法';
            $data['data'] = [];
            return json_encode($data);
        }

        $goods = DB::table('mall_goods')->where('id', $goods_id)->first();
        if (empty($goods)) {
            $data['code'] = 1;
            $data['msg'] = '请求的商品不存在';
            $data['data'] = [];
            return json_encode($data);
        }

        $user_id = $this->getLoginUser();
        $xyb = $goods->need_xyb / 100;

        $couponList = DB::table('user_coupon')
            ->select('code', 'name', 'min_price', 'max_value', 'remark', 'status')
            ->where([
                ['user_id', $user_id],
                ['status', 1],
            ])
            ->where('max_value', '>=', $xyb)
            ->orderBy('id', 'desc')
            ->get();

        $data['code'] = 0;
        $data['data'] = [];
        foreach ($couponList as $key => $coupon) {
            $data['data'][$key]['code'] = $coupon->code;
            $data['data'][$key]['name'] = $coupon->name;
            $data['data'][$key]['status'] = $coupon->status;
            $data['data'][$key]['max_value'] = $coupon->max_value * 100;
            $data['data'][$key]['remark'] = "可用于兑换" . ($coupon->max_value * 100) . "小妖币以下的商品";
        }
        return json_encode($data);
    }

    /**
     * 取当前用户优惠卷
     * @param Request $request
     * @return mixed
     */
    public function userCoupon(Request $request)
    {
        $user_id = $this->getLoginUser();
        $couponList = DB::table('user_coupon')
            ->select('code', 'name', 'min_price', 'max_value', 'remark', 'status')
            ->where([
                ['user_id', $user_id],
            ])
            ->whereIn('status', [1, 2])
            ->orderBy('id', 'desc')
            ->get();

        $data['code'] = 0;
        $data['data'] = [];
        foreach ($couponList as $key => $coupon) {
            $data['data'][$key]['code'] = $coupon->code;
            $data['data'][$key]['name'] = $coupon->name;
            $data['data'][$key]['status'] = $coupon->status;
            $data['data'][$key]['max_value'] = $coupon->max_value * 100;
            $data['data'][$key]['remark'] = "可用于兑换" . ($coupon->max_value * 100) . "小妖币以下的商品";
        }
        return json_encode($data);
    }


    /**
     * 给用户发放优惠卷
     * @param Request $request
     * @return mixed
     */
    public function giveCouponUser(Request $request)
    {
        $typeId = $request->input('typeId'); //当前商品ID
        $user_id = self::getLoginUser();

        // 检查请求参数
        if (empty($typeId) || !is_numeric($typeId)) {
            $data['code'] = 1;
            $data['msg'] = '请求参数不合法';
            $data['data'] = [];
            return json_encode($data);
        }

        //取得随机码
        $key = "csy:user_" . $user_id;
        if (Cache::has($key)) {
            $randId = Cache::pull($key);
        }else{
            $data['code'] = 1;
            $data['msg'] = '没达到赠送要求!';
            $data['data'] = [];
            return json_encode($data);
        }

        //再次判断是否赠送优惠卷条件
        $user = new \App\Tools\User\User();
        $is_show = $user->getUserIsCoupon($user_id, $randId);
        if (empty($is_show)) {
            $data['code'] = 1;
            $data['msg'] = '请求无效';
            $data['data'] = [];
            return json_encode($data);
        }
        //发送优惠卷
        return $user->giveCouponUser($user_id);

    }

    /**
     * 验证登录
     *
     * @return [type] [description]
     */
    private function getLoginUser()
    {
        // 检查用户登录态
        $user = auth()->user();
        $uid = $user->id;
        return $uid;
    }
}
